package com.pro.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

public class StringUtils {
	/**
	 * 判断字符串是否为�?
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return null == str  || str.equals("")
				|| str.matches("\\s*");
	}

	/**
	 * 给输入的字符串设置默认�?
	 * @param content
	 * @param defaultValue
	 * @return
	 */
	public static String defaultValue(String content,String defaultValue){
		if(isEmpty(content)){
			return defaultValue;
		}
		return content;
	}
	
	/**
	 * 把数据库字段名改为驼峰方�?
	 * @param column
	 * @return
	 */
	public static String columnToProperty(String column) {
		/**如果字段名为空，就返回空字符�? */
		if(isEmpty(column)) return "";
		/**获取字段的长度，�?��来说字段长度不可能有几百个字节的，所以用Byte就行�? */
		Byte length = (byte) column.length();
		
	    StringBuilder sb = new StringBuilder(length);
	    int i = 0;
	    /**遍历字段的每�?��字符* */
	    for (int j = 0; j < length; j++) {
	    	 /**匹配到第�?��_* */
	        if (column.charAt(j) == '_') {
	            /**如果后面还有_,也就是连续的_,那么j就需要自增一个单位，直到后面不是_为止* */
	            while (column.charAt(j + 1) == '_') {
	            	j += 1;
	            }
	            sb.append(("" + column.charAt(++j)).toUpperCase());
	            
	        } else {
	        	 /**如果循环到的字符不是_,那么就保存下�? */
	                sb.append(column.charAt(j));
	            }
	        }
	  
        return sb.toString();
    }
	    
    /**
	 * 将一个字符串的首字母改成大写
	 * @param str
	 * @return
	 */
	public static String upperCaseFirstCharacter(String str){
		StringBuilder sb = new StringBuilder();
		char[] arr = str.toCharArray();
		for (int i = 0; i < arr.length; i++) {
			if(i==0) sb.append((arr[i] + "").toUpperCase());
			else sb.append((arr[i]+""));
		}
		return sb.toString();
	}
	
	 public static String firstCharacterLowerCase(String str) {
	        String firstCharacter = String.valueOf(str.charAt(0)).toLowerCase();
	        return firstCharacter + str.substring(1, str.length());
	    }

	    public static Object[] extendArray(Object[] array, Object... objects) {
	        Object[] newArray = null;
	        if (array.length < 1) {
	            return objects;
	        } else {
	            newArray = new Object[array.length + objects.length];

	            int i;
	            for (i = 0; i < array.length; ++i) {
	                newArray[i] = array[i];
	            }

	            for (i = 0; i < objects.length; ++i) {
	                newArray[array.length + i] = objects[i];
	            }

	            return newArray;
	        }
	    }

	    public static String getUUID() {
	        return UUID.randomUUID().toString();
	    }

	    public static Object[] listToObjectArray(List<Object> list) {
	        Object[] objArr = new Object[list.size()];

	        for (int i = 0; i < objArr.length; ++i) {
	            objArr[i] = list.get(i);
	        }

	        return objArr;
	    }

	    public static String nullToEmpty(Object o) {
	        return o != null ? o.toString() : "";
	    }

	    public static String join(List<String> list) {
	        String str = "";

	        for (int i = 0; i < list.size(); ++i) {
	            str = str + (String) list.get(i) + ",";
	        }

	        return str.substring(0, str.length() - 1);
	    }

	    public static boolean isEmpty(Object o) {
	        String str = nullToEmpty(o);
	        return str == null || str.equals("") || str.matches("\\s*");
	    }

	    public static boolean isNotEmpty(Object o) {
	        return !isEmpty(o);
	    }

	   

	   

	    public static String formatDate(Date date, String format) {
	        String result = "";
	        SimpleDateFormat sdf = new SimpleDateFormat(format);
	        if (date != null) {
	            result = sdf.format(date);
	        }

	        return result;
	    }

	    public static Date formatString(String str, String format) throws Exception {
	        SimpleDateFormat sdf = new SimpleDateFormat(format);
	        return sdf.parse(str);
	    }
	    
	    public static String getToday() {
	        return formatDate(new Date(), "yyyy-MM-dd");
	    }
	    
	    /**
	     * 获取当前日期是一年中的第几周
	     * @return
	     */
	    public static int getWeek(){
	    	Calendar cal = Calendar.getInstance();
	    	cal.setFirstDayOfWeek(Calendar.MONDAY);
	    	cal.setTime(new Date());
	    	
	    	return cal.get(Calendar.WEEK_OF_YEAR);
	    }

	    public static String getCurrentDateForMysql() {
	        return formatDate(new Date(), "yyyy-MM-dd hh:mm:ss");
	    }

	    public static String getCurrentDateForMysql24() {
	        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	    }
    
	    
	    public static Map getParameterMap(HttpServletRequest request) {  
	        // 参数Map  
	        Map properties = request.getParameterMap();  
	        // 返回值Map  
	        Map returnMap = new HashMap();  
	        Iterator entries = properties.entrySet().iterator();  
	        Map.Entry entry;  
	        String name = "";  
	        String value = "";  
	        while (entries.hasNext()) {  
	            entry = (Map.Entry) entries.next();  
	            name = (String) entry.getKey();  
	            Object valueObj = entry.getValue();  
	            if(null == valueObj){  
	                value = "";  
	            }else if(valueObj instanceof String[]){  
	                String[] values = (String[])valueObj;  
	                for(int i=0;i<values.length;i++){  
	                    value = values[i] + ",";  
	                }  
	                value = value.substring(0, value.length()-1);  
	            }else{  
	                value = valueObj.toString();  
	            }  
	            returnMap.put(name, value);  
	        }  
	        return returnMap;  
	    }  
}
